﻿using System;
using System.Collections;
using System.IO;
namespace Tickz.Cachez
{
    /// <summary>
    /// 数据缓存保存信息异步处理委托
    /// </summary>
    delegate void EventSaveCache(object key, object value);
    /// <summary>
    /// 动软的缓存
    /// 管理类
    /// </summary>
    internal class CodematicCacheStrategy : ICacheStrategy
    {   
        // 用于缓存数据的Hashtable       
        private  System.Collections.Hashtable _Cache = new System.Collections.Hashtable();
        /// <summary>
        /// 默认缓存存活期为3600秒(1小时)
        /// </summary>
        protected int _timeOut = 3600;
        private static object syncObj = new object();        
        /// <summary>
        /// 构造函数
        /// </summary>
        static CodematicCacheStrategy()
        {            
            
        }
        public CodematicCacheStrategy()
        {
            
        }
        /// <summary>
        /// 设置到期相对时间[单位: 秒] 
        /// </summary>
        public virtual int TimeOut
        {
            set { _timeOut = value > 0 ? value : 3600; }
            get { return _timeOut > 0 ? _timeOut : 3600; }
        }


        /// <summary>
        /// 加入当前对象到缓存中
        /// </summary>
        /// <param name="objId">对象的键值</param>
        /// <param name="o">缓存的对象</param>
        public virtual void AddObject(string objId, object o)
        {            
            AddObject(objId,o,0);
        }

        /// <summary>
        /// 加入当前对象到缓存中
        /// </summary>
        /// <param name="objId">对象的键值</param>
        /// <param name="o">缓存的对象</param>
        /// <param name="o">到期时间,单位:秒</param>
        public virtual void AddObject(string objId, object o, int expire)
        {
            if (String.IsNullOrEmpty(objId) || o == null)
            {
                return;
            }
            lock (syncObj)
            {
                if (!_Cache.ContainsKey(objId))
                    _Cache.Add(objId, o);
            }  
        }


        /// <summary>
        /// 加入当前对象到缓存中,并对相关文件建立依赖
        /// </summary>
        /// <param name="objId">对象的键值</param>
        /// <param name="o">缓存的对象</param>
        /// <param name="files">监视的路径文件</param>
        public virtual void AddObjectWithFileChange(string objId, object o, string[] files)
        {
            if (String.IsNullOrEmpty(objId) || o == null)
            {
                return;
            }
            foreach (string filePath in files)
            {
                //文件监视类
                FileSystemWatcher fileWatcher = new FileSystemWatcher();
                fileWatcher.Path = Path.GetFullPath(filePath);
                fileWatcher.Filter = Path.GetFileName(filePath);
                //监视文件或文件夹的属性类型
                fileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
                //绑定事件
                fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
                fileWatcher.EnableRaisingEvents = true;
            }
        }
        /// <summary>
        /// 监听文件改变 事件
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private void OnChanged(object source,FileSystemEventArgs e)
        {            
            RemoveObject("");
        }

        /// <summary>
        /// 加入当前对象到缓存中,并使用依赖键
        /// </summary>
        /// <param name="objId">对象的键值</param>
        /// <param name="o">缓存的对象</param>
        /// <param name="dependKey">依赖关联的键值</param>
        public virtual void AddObjectWithDepend(string objId, object o, string[] dependKey)
        {
            if (String.IsNullOrEmpty(objId) || o == null)
            {
                return;
            }

           
        }

      
        /// <summary>
        /// 删除缓存对象
        /// </summary>
        /// <param name="objId">对象的关键字</param>
        public virtual void RemoveObject(string objId)
        {
            if (String.IsNullOrEmpty(objId))
            {
                return;
            }
            lock (_Cache.SyncRoot)
            {
                _Cache.Remove(objId);
            }
        }


        /// <summary>
        /// 返回一个指定的对象
        /// </summary>
        /// <param name="objId">对象的关键字</param>
        /// <returns>对象</returns>
        public virtual object RetrieveObject(string objId)
        {
            if (String.IsNullOrEmpty(objId))
            {
                return null;
            }
            if (_Cache.ContainsKey(objId))
            {
                return _Cache[objId];
            }
            return null;
        }

        /// <summary>
        /// 清空的有缓存数据
        /// </summary>
        public virtual void FlushAll()
        {
            lock (_Cache.SyncRoot)
            {
                _Cache.Clear();
            }
        }

        /// <summary>
        /// 建立回调委托的一个实例
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="reason"></param>
        public void onRemove(string key, object val)
        {
            			
        }

    }
}
